package com.example.testplatform.controller;

import com.example.testplatform.common.LogUtils;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 欢迎页面控制器
 * 处理根路径请求，提供简单的欢迎信息
 */
@RestController
public class WelcomeController {

    private static final Logger logger = LogUtils.getLogger(WelcomeController.class);

    /**
     * 处理根路径请求
     * @return 欢迎信息
     */
    @GetMapping("/")
    public String welcome() {
        long startTime = System.currentTimeMillis();
        LogUtils.logApiRequest(logger, "GET", "/", "欢迎页面请求处理开始");
        LogUtils.logSystemEvent(logger, "WELCOME_PAGE_ACCESS", "用户访问欢迎页面");
        LogUtils.info(logger, () -> "接收到根路径请求，开始处理欢迎页面");
        LogUtils.debug(logger, () -> "开始构建欢迎页面内容");
        
        try {
            // 构建欢迎页面内容
            String welcomeMessage = "<h1>测试平台后端服务已启动</h1>\n" +
                                   "<p>请通过前端开发服务器访问完整功能：<a href='http://localhost:8081'>http://localhost:8081</a></p>\n" +
                                   "<p>API文档：<a href='http://localhost:8080/swagger-ui.html'>Swagger UI</a></p>";
            
            LogUtils.debug(logger, () -> "欢迎页面内容构建完成，内容长度=" + welcomeMessage.length() + "字符");
            LogUtils.info(logger, () -> "欢迎页面请求处理完成，准备返回响应");
            LogUtils.logPerformance(logger, "welcome", startTime, System.currentTimeMillis());
            LogUtils.logSystemEvent(logger, "WELCOME_PAGE_SERVED", "欢迎页面请求处理成功，耗时=" + (System.currentTimeMillis() - startTime) + "ms");
            
            return welcomeMessage;
        } catch (Exception e) {
            LogUtils.logApiError(logger, "GET", "/", e);
            LogUtils.error(logger, "处理欢迎页面请求异常: " + LogUtils.formatException(e));
            LogUtils.logSystemEvent(logger, "WELCOME_PAGE_ERROR", "处理欢迎页面请求异常: " + e.getMessage());
            LogUtils.logPerformance(logger, "welcome", startTime, System.currentTimeMillis());
            
            // 即使出错也返回一个简单的欢迎消息，确保前端能看到反馈
            return "<h1>测试平台后端服务运行中</h1><p>服务正常但欢迎页面渲染过程中出现异常</p>";
        }
    }
}